package cn.com.lms.business.questionnaire.web;

import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.learning.persistence.entity.FbLearningStudent;
import cn.com.lms.business.learning.service.LearningPlanService;
import cn.com.lms.business.learning.service.LearningStudentService;
import cn.com.lms.business.questionnaire.persistence.entity.UserQuestionnaire;
import cn.com.lms.business.questionnaire.service.UserQuestionnaireService;
import cn.com.lms.business.user.persistence.entity.SysUserVo;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: 16266
 * @DateTime: 2020/3/24 13:49
 * @Description: 填写问卷调查
 */
@Tag(name = "问卷填写", description = "用户调查问卷填写")
@RestController
@RequestMapping(value = "/questionnaire/user")
public class UserQuestionnaireController {

    @Autowired
    private UserQuestionnaireService userQuestionnaireService;


    @Autowired
    private LearningStudentService learningStudentService;



    @Operation(summary = "用户问卷填写", responses = {
            @ApiResponse(responseCode = "1", description = "新增成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseResult.class))),
            @ApiResponse(responseCode = "0", description = "新增失败")})
    @PostMapping(value = "/add")
    public ResponseResult<UserQuestionnaire> addQuestionnaireInfo(@RequestBody UserQuestionnaire userQuestionnaire) {
        SysUserVo currentUser = SystemUserUtil.getCurrentUser();
        EntityUtils.saveBefore(userQuestionnaire, currentUser.getAccount(), currentUser.getCname());
        //添加用户ID
        userQuestionnaire.setUserId(currentUser.getId());
        FbLearningStudent learningStudent  = learningStudentService.findByUserIdAndStageDetailId(currentUser.getId(),userQuestionnaire.getStageDetailId());
        learningStudent.setFinishState("1");
        learningStudent.setLastStudyTime(new Date());
        userQuestionnaireService.addUserQuestionaire(learningStudent,userQuestionnaire,userQuestionnaire.getPlanId());
        return ResponseResult.success();
    }

}
